package com.example.zookeeper1;


import com.example.zookeeper1.demos.DataWatch;
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.CountDownLatch;

@SpringBootTest
public class ZkTest1 {

    @Autowired
    private ZooKeeper zooKeeper;



    @Test
    void create1() throws InterruptedException, KeeperException {
        String s = zooKeeper.create("/usa2", "tunp".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.CONTAINER);
        System.out.println(s);
    }

    @Test
    void createSequental() throws InterruptedException, KeeperException {
        String s = zooKeeper.create("/task2", "test2".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
        System.out.println(s);
    }

    @Test
    void get1() throws InterruptedException, KeeperException {
        byte[] data = zooKeeper.getData("/usa2", false, new Stat());
        System.out.println(new String(data));
    }

    @Test
    void set1() throws InterruptedException, KeeperException {
        Stat stat = zooKeeper.setData("/usa", "update val 2".getBytes(), 2);
        System.out.println(stat);
    }
    @Test
    void get2() throws InterruptedException, KeeperException {
        Stat stat = new Stat();
        zooKeeper.getData("/usa",false,stat);
        System.out.println(stat.getVersion());
    }
    @Test
    void getChildren() throws InterruptedException, KeeperException {
        List<String> children = zooKeeper.getChildren("/china", false);
        System.out.println(children);
    }
    @Test
    void getAndWatch() throws InterruptedException, KeeperException, IOException {
        byte[] data = zooKeeper.getData("/china", (event) -> {
            System.out.println("=================");
            System.out.println(event);
            System.out.println(event.getType() == Watcher.Event.EventType.NodeDataChanged);
        }, null);
        System.in.read();
        System.out.println(data);
    }

    @Test
    void childrenWatch() throws InterruptedException, KeeperException, IOException {
        zooKeeper.getChildren("/china",(event -> {
            System.out.println(event);
        }));
        System.in.read();

    }

    @Test
    void getStudy1() throws InterruptedException, KeeperException {
        byte[] data = zooKeeper.getData("/bab", null, null);
        System.out.println(new String(data));
    }

    @Test
    void createSequence1() throws InterruptedException, KeeperException {
//        byte[] data = zooKeeper.getData("/se/s1", null, null);
//        System.out.println(new String(data));
        String s = zooKeeper.create("/se/order", "test2".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
        System.out.println(s);
    }

    @Test
    void alwaysWatch() throws InterruptedException, KeeperException {
        CountDownLatch downLatch = new CountDownLatch(1);

        DataWatch dataWatch = new DataWatch(zooKeeper, "/demo3");
        dataWatch.watchDataChange();

        downLatch.await();
    }
}
